//
//  HomeViewController.swift
//  LYBluetoothKit
//
//  Created by 李永彬 on 2020/5/18.
//  Copyright © 2020 李永彬. All rights reserved.
//

import UIKit

class HomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = .white
        
        let bluetoothBtn = UIButton(type: .custom)
        bluetoothBtn.addTarget(self, action: #selector(push), for: .touchUpInside)
        bluetoothBtn.setTitle("Bluetooth", for: .normal)
        bluetoothBtn.backgroundColor = UIColor.red
        bluetoothBtn.titleLabel?.textColor = UIColor.white
        bluetoothBtn.frame = CGRect(x: 150, y: 200, width: 100, height: 40)
        bluetoothBtn.center = self.view.center
        self.view.addSubview(bluetoothBtn)
               
        NotificationCenter.default.addObserver(self, selector: #selector(connectedBluetooth(noti:)), name: NSNotification.Name(rawValue: LYBluetoothConnectedFromRestoreStateNotification), object: nil)
    }
    
    @objc func push() {
        if LYCentral.shared.state == .LYBluetoothStateConnected {
            let unitsVC = SetUnitsViewController()
            self.navigationController?.pushViewController(unitsVC, animated: true)
        } else {
            let VC = BluetoothViewController()
            self.navigationController?.pushViewController(VC, animated: true)
        }
    }
    
    @objc func connectedBluetooth(noti:NSNotification) {
        let peripheral: LYRemotePeripheral = noti.userInfo?["peripheral"] as! LYRemotePeripheral
        let error: Error? = noti.userInfo?["error"] as? Error
        print(peripheral.peripheral?.name ?? "没有蓝牙名")
        guard error == nil else {
            return
        }
        UIApplication.shared.keyWindow!.makeToast("连接设备成功")
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
